#!/bin/sh

if [ "$1" == "--help" ] ; then
	echo
	echo " --------------------------------------------------------------"
	echo "  format: sh $0 <php/php53m/php54m/php55m>"
	echo " --------------------------------------------------------------"
	echo "   - 'php' mean 'php branch' (check with 'php v')"
	echo "   - for 'phpXYm', check with 'php55m-cli v' (for php55m)"
	echo "   - unsupport for 'php52m'"
	echo
	exit;
fi

echo
echo "- For help, type 'sh $0 --help'"

if [ "$#" == 0 ] ; then
	echo "- No argument supplied. Defaulting to localhost (master) servers"
	php_target="php"
else
	php_target=$1
fi

echo

kpath="/usr/local/lxlabs/kloxo"
eripath="/etc/rc.d/init.d"

if [ -f ${kpath}/init/custom.php-fpm.init.base ] ; then
	pcfile="${kpath}/init/custom.php-fpm.init.base"
else
	pcfile="${kpath}/init/php-fpm.init.base"
fi

if [ -f ${kpath}/etc/flag/enablemultiplephp.flg ] ; then
	echo "- 'Multiple PHP' is enable. Using it"

	'cp' -f /opt/configs/php-fpm/etc/init.d/phpm-fpm.init ${eripath}/phpm-fpm
	chown root:root ${eripath}/phpm-fpm
	chmod 0755 ${eripath}/phpm-fpm
	chkconfig phpm-fpm on
	sh /script/add-restart-queue restart-php-fpm
else
	chkconfig phpm-fpm off
	service phpm-fpm stop >/dev/null 2>&1
fi

if [ "${php_target}" == "php" ] ; then
	echo "- No argument and then use 'PHP Branch'"

	'cp' -f ${pcfile} ${eripath}/php-fpm
	sed -i 's:__custom_name__:'${php_target}':' ${eripath}/php-fpm
	chown root:root ${eripath}/php-fpm
	chmod 0755 ${eripath}/php-fpm
	chkconfig php-fpm on
	sh /script/add-restart-queue restart-php-fpm
else
	if [ ! -f /opt/${php_target}/usr/sbin/php-fpm ] ; then
		echo "- Need ${php_target} installing with 'sh /script/phpm-installer ${php_target}'"
	else
		echo "- Set ${php_target} as php-fpm target for 'PHP Used'"

		'cp' -f ${pcfile} ${eripath}/php-fpm
		sed -i 's:__custom_name__:'${php_target}':' ${eripath}/php-fpm
		chown root:root ${eripath}/php-fpm
		chmod 0755 ${eripath}/php-fpm
		chkconfig php-fpm on
		sh /script/add-restart-queue restart-php-fpm
	fi
fi

rm -f ${kpath}/etc/flag/use_php*.flg
echo '' > ${kpath}/etc/flag/use_${php_target}.flg